package com.muqi.app.qlearn.fragments;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.formatter.LargeValueFormatter;
import com.muqi.app.network.http.GlideImageUtils;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.MContants;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.NumberFormatUtil;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.adapter.HomeWorkAdapter;
import com.muqi.app.qlearn.modles.Banner;
import com.muqi.app.qlearn.modles.GrowData;
import com.muqi.app.qlearn.modles.GrowRewardAndMoveInfo;
import com.muqi.app.qlearn.modles.HomeWorkBean;
import com.muqi.app.qlearn.modles.HomeWorkFile;
import com.muqi.app.qlearn.modles.LastRecord;
import com.muqi.app.qlearn.modles.StudyInfo;
import com.muqi.app.qlearn.student.BaseChildFragment;
import com.muqi.app.qlearn.student.BaseFragment;
import com.muqi.app.qlearn.student.MenuActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.ui.HomeWorkStatisticActivity;
import com.muqi.app.qlearn.student.ui.WeakActivity;
import com.muqi.app.qlearn.ui.MyHomeWkDetailsActivity;
import com.muqi.app.qlearn.ui.MyHomeWorkActivity;
import com.muqi.app.user.db.ChildBean;
import com.muqi.app.user.widget.SelectableRoundedImageView;
import com.muqi.app.widget.pulltorefresh.XListView;
import com.muqi.app.widget.scanpicture.ScanPictureActivity;

@SuppressLint("InflateParams")
public class AcademicAnalysisFragment extends BaseChildFragment implements
		OnClickListener {
	private RadarChart mRadarChart_knowledge;
	private RadarChart mRadarChart_ability;
	private RadarChart mRadarChart_skill;
	private TextView mTvKonwlege;
	private TextView mTvAbility;
	private TextView mTvSkill;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_academic_level, null);
		initView(view);
		return view;
	}

	private void initView(View view) {

		mRadarChart_knowledge = (RadarChart) view
				.findViewById(R.id.konwledge_chart);
		mRadarChart_ability = (RadarChart) view
				.findViewById(R.id.ability_chart);
		mRadarChart_skill = (RadarChart) view
				.findViewById(R.id.technology_chart);
		mTvKonwlege = (TextView) view.findViewById(R.id.tv_konwledge);
		mTvAbility = (TextView) view.findViewById(R.id.tv_ability);
		mTvSkill = (TextView) view.findViewById(R.id.tv_skill);

		mTvKonwlege.setOnClickListener(this);
		mTvAbility.setOnClickListener(this);
		mTvSkill.setOnClickListener(this);
		loadData();

	}

	/**
	 * 加载数据
	 */
	private void loadData() {
		getDateStatis(0);// 能力
		getDateStatis(1);// 知识
	}

	/**
	 * 获奖统计 综合素质
	 */
	private void getDateStatis(final int type) {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		// sendMap.put("school_id", mSpUtil.getChildSchoolId());
		sendMap.put("grade_id", mSpUtil.getChildGradeId());
		sendMap.put("class_manager_id", mSpUtil.getChildClassId());
		sendMap.put("student_id", mSpUtil.getCurrentChildId());
		sendMap.put("subject_id", "34");
		sendMap.put("check_type", type);
		String requestUrl = ParamsUtils.buildParams(
				NetWorkApi.GET_STUDY_STA_API, sendMap);
		MyAsyncHttp.get(mActivity, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				if (resuleCode == RESULTCODE.SUCCESS) {
					List<StudyInfo> infos = ResponseUtils.getStudyInfos(
							mActivity, result);

					if (infos != null) {
						if (type == 0) {
							showChart(mRadarChart_ability, infos);
						} else if (type == 1) {
							showChart(mRadarChart_knowledge, infos);
						}

					}

				}

			}

		});

	}

	private void showChart(RadarChart mChart, List<StudyInfo> statisticsList2) {

		RadarData data = getPieData(statisticsList2);

		mChart.setNoDataTextDescription("还没有相关的数据");
		mChart.setDescription("");
		mChart.setRotationEnabled(false);

		mChart.setWebLineWidth(1.5f);
		mChart.setWebColor(Color.LTGRAY);
		mChart.setWebLineWidthInner(0.75f);
		mChart.setWebAlpha(100);
		mChart.setData(data);
		mChart.invalidate();

		mChart.animateXY(1400, 1400, Easing.EasingOption.EaseInOutQuad,
				Easing.EasingOption.EaseInOutQuad);

		XAxis xAxis = mChart.getXAxis();
		xAxis.setTextSize(9f);
		xAxis.setTextColor(Color.parseColor("#AFAFAF"));
		
		YAxis yAxis = mChart.getYAxis();
		yAxis.setLabelCount(6, true);
		yAxis.setTextSize(0f);
		yAxis.setTextColor(Color.parseColor("#00DEDEDE"));
		yAxis.setAxisMinValue(0f);
		yAxis.setDrawLabels(false);
		

		Legend l = mChart.getLegend();
		l.setPosition(LegendPosition.BELOW_CHART_CENTER);
		l.setTextColor(Color.parseColor("#AFAFAF"));
		l.setForm(LegendForm.SQUARE);
		l.setXEntrySpace(7f);
		l.setYEntrySpace(5f);
		mChart.animate();
	}

	/**
	 * 
	 * @param count
	 *            分成几部分
	 * @param range
	 */

	private RadarData getPieData(List<StudyInfo> statisticsList) {

		ArrayList<String> xValues = new ArrayList<String>(); // xVals用来表示每个饼块上的内容
		ArrayList<Entry> yValues = new ArrayList<Entry>(); // 个人
		ArrayList<Entry> yValues2 = new ArrayList<Entry>(); // 班级
		ArrayList<Entry> yValues3 = new ArrayList<Entry>(); // 年级
		for (int i = 0; i < statisticsList.size(); i++) {
			StudyInfo data = statisticsList.get(i);
			if (data != null) {
				xValues.add(statisticsList.get(i).name);

				yValues.add(new Entry(NumberFormatUtil
						.getFloatFromString(data.person_score), i));

				yValues2.add(new Entry(NumberFormatUtil
						.getFloatFromString(data.class_score), i));
				yValues3.add(new Entry(NumberFormatUtil
						.getFloatFromString(data.grade_score), i));

			}

		}

		// y轴的集合
		RadarDataSet pieDataSet = new RadarDataSet(yValues, "个人");// "Quarterly Revenue 2014"/*显示在比例图上*/);
		pieDataSet.setColor(Color.rgb(232, 52, 40));

		pieDataSet.setDrawFilled(false);
		pieDataSet.setLineWidth(2f);
		pieDataSet.setDrawValues(false);
		
		RadarDataSet pieDataSet2 = new RadarDataSet(yValues2, "班级");// "Quarterly Revenue 2014"/*显示在比例图上*/);

		pieDataSet2.setColor(Color.rgb(40, 167, 225));

		pieDataSet2.setDrawFilled(false);
		pieDataSet2.setLineWidth(2f);
		pieDataSet2.setDrawValues(false);

		RadarDataSet pieDataSet3 = new RadarDataSet(yValues3, "年级");// "Quarterly Revenue 2014"/*显示在比例图上*/);

		pieDataSet3.setColor(Color.rgb(20, 174, 102));

		pieDataSet3.setDrawFilled(false);
		pieDataSet3.setLineWidth(2f);
		pieDataSet3.setDrawValues(false);

		ArrayList<RadarDataSet> sets = new ArrayList<RadarDataSet>();
		sets.add(pieDataSet);
		sets.add(pieDataSet2);
		sets.add(pieDataSet3);

		RadarData data = new RadarData(xValues, sets);

		data.setValueTextSize(8f);
		data.setValueFormatter(new LargeValueFormatter());

		return data;
	}

	@Override
	protected void onChoiceChildResult(ChildBean myChild) {
		// TODO Auto-generated method stub

	}

	/**
	 * 根据选择的学科加载对应的数据
	 */
	public void changeSubject() {
		loadData();

	}

	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent(mActivity, WeakActivity.class);
		switch (arg0.getId()) {
		case R.id.tv_konwledge:
			intent.putExtra("type", 0);
			startActivity(intent);
			break;
		case R.id.tv_ability:
			intent.putExtra("type", 1);
			startActivity(intent);
			break;
		case R.id.tv_skill:
			intent.putExtra("type", 2);
			startActivity(intent);
			break;
		default:
			break;
		}

	}
}